package com.bjqg.bjqg01.bjqg.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.bean.Client;
import com.bjqg.bjqg01.bjqg.utils.Constant;
import com.bjqg.bjqg01.bjqg.utils.GreendaoUtils;
import com.bjqg.greendao.ClientDao;

/**
 * Created by bjqg01 on 2017/12/14.
 */

public class ClientDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageView goback;
    private TextView toptitle;
    private ImageView select;
    private TextView sendClient;
    private EditText engineerName;
    private EditText clientName;
    private EditText phoneNum;
    private EditText clientCompany;
    private EditText buildCompany;
    private EditText samplePerson;
    private EditText sampleCompany;
    private EditText witnessPerson;
    private EditText witnessCompany;
    private String getername;
    private String getctname;
    private String getpenum;
    private String getctcy;
    private String getbdcy;
    private String getsepn;
    private String getsecy;
    private String getwspn;
    private String getwscy;
    private Client extra;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        extra = (Client) getIntent().getSerializableExtra("client");
        extra = getIntent().getParcelableExtra(Constant.CLIENTMANAGETODETAIL);
//        Log.e("123456789", extra.getClient().toString());
        setContentView(R.layout.activity_client_detail);
        initView();
        initSetView();

    }

    private void initSetView() {
        if (extra==null){
            return;
        }

        engineerName.setText(extra.getProjectName().toString());
        clientName.setText(extra.getClient().toString());
        phoneNum.setText(extra.getClientPhoneNum().toString());
        clientCompany.setText(extra.getClientCompany().toString());
        buildCompany.setText(extra.getBuildCompany().toString());
        samplePerson.setText(extra.getSamplePerson().toString());
        sampleCompany.setText(extra.getSampleCompany().toString());
        witnessPerson.setText(extra.getWitnessPerson().toString());
        witnessCompany.setText(extra.getWitnessCompany().toString());
    }

    private void initView() {
        goback = (ImageView) findViewById(R.id.back);
        toptitle = (TextView) findViewById(R.id.tv_top_title);
        select = (ImageView) findViewById(R.id.select);
        sendClient = (TextView) findViewById(R.id.send_client);

        engineerName = (EditText) findViewById(R.id.detail_engineer_name);
        clientName = (EditText) findViewById(R.id.detail_client_name);
        phoneNum = (EditText) findViewById(R.id.detail_client_phone_num);
        clientCompany = (EditText) findViewById(R.id.detail_client_company);
        buildCompany = (EditText) findViewById(R.id.detail_build_company);
        samplePerson = (EditText) findViewById(R.id.detail_sample_person);
        sampleCompany = (EditText) findViewById(R.id.detail_sample_company);
        witnessPerson = (EditText) findViewById(R.id.detail_witness_person);
        witnessCompany = (EditText) findViewById(R.id.detail_witness_company);

        toptitle.setText("委托人信息");
        select.setImageResource(R.mipmap.delete);
        select.setOnClickListener(this);
        goback.setOnClickListener(this);
        sendClient.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.send_client:

                getTextString();

                if (extra==null){
//                    Client client = new Client(null, "张三", "18812345678", "好单位", "好工程", "给力", "", "", "", "", false);
                    Client client = new Client(null, getername,
                            getctname, getpenum, getctcy, getbdcy, getsepn, getsecy, getwspn, getwscy, false);
                    GreendaoUtils.getDaoSession(getApplicationContext(),Constant.CLIENTDETAILDBNAME).getClientDao().insert(client);
                }else {
                    ClientDao clientDao = GreendaoUtils.getDaoSession(getApplicationContext(),Constant.CLIENTDETAILDBNAME).getClientDao();
                    Client client = clientDao.queryBuilder()
                            .where(ClientDao.Properties.Id.eq(extra.getId())).build().unique();
                    client.setClient(getctname);
                    client.setClientPhoneNum(getpenum);
                    client.setClientCompany(getctcy);
                    client.setBuildCompany(getbdcy);
                    client.setSamplePerson(getsepn);
                    client.setSampleCompany(getsecy);
                    client.setWitnessPerson(getwspn);
                    client.setWitnessCompany(getwscy);
                    client.setProjectName(getername);
                    clientDao.update(client);
                }

//                extra=null;
                finish();
                break;
            case R.id.select:

                showNormalDialog();
                break;
        }
    }



    private void getTextString() {
        getername = engineerName.getText().toString().trim();
        getctname = clientName.getText().toString().trim();
        getpenum = phoneNum.getText().toString().trim();
        getctcy = clientCompany.getText().toString().trim();
        getbdcy = buildCompany.getText().toString().trim();
        getsepn = samplePerson.getText().toString().trim();
        getsecy = sampleCompany.getText().toString().trim();
        getwspn = witnessPerson.getText().toString().trim();
        getwscy = witnessCompany.getText().toString().trim();

//        if (getername.trim()==null&&getername.trim().equals(""))showToast("工程名称为必填");
//        if (getctname.trim()==null&&getctname.trim().equals(""))showToast("委托人为必填");
//        if (getpenum.trim()==null&&getpenum.trim().equals(""))showToast("委托人电话为必填");
//        if (getctcy.trim()==null&&getctcy.trim().equals(""))showToast("委托人单位为必填");
    }

    private void showToast(String s) {
        Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
    }

    private void showNormalDialog(){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("删除");
        normalDialog.setMessage("是否删除当前记录?");
        normalDialog.setPositiveButton("是",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        ClientDao clientDao = GreendaoUtils.getDaoSession(getApplicationContext(),Constant.CLIENTDETAILDBNAME).getClientDao();

                        Client client = clientDao.queryBuilder()
                                .where(ClientDao.Properties.Id.eq(extra.getId())).build().unique();
                        clientDao.delete(client);
                        finish();
                    }
                });
        normalDialog.setNegativeButton("否",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }

}
